package io.renren.modules.course.controller;

import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.modules.course.entity.SchoolEntity;
import io.renren.modules.course.service.SchoolService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;



/**
 * 学校管理
 *
 * @author zlb
 * @email 316682546@qq.com
 * @date 2018-08-14 13:56:26
 */
@RestController
@RequestMapping("course/school")
public class SchoolController extends AbstractController {
    @Autowired
    private SchoolService schoolService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("course:school:list")
    public List<SchoolEntity> list(){
        List<SchoolEntity> deptList = schoolService.queryList(new HashMap<String, Object>());

        return deptList;
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
//    @RequiresPermissions("course:school:select")
    public R select(){
        List<SchoolEntity> deptList = schoolService.queryList(new HashMap<String, Object>());

        //添加一级部门
        if(getUserId() == Constant.SUPER_ADMIN){
            SchoolEntity root = new SchoolEntity();
            root.setId(0L);
            root.setName("学校");
            root.setParentId(-1L);
            root.setOpen(true);
            deptList.add(root);
        }

        return R.ok().put("deptList", deptList);
    }


    /**
     * 获得学校列表
     */
    @RequestMapping("/getSchoolTree")
//    @RequiresPermissions("course:school:select")
    public R getSchoolTree(){
        List<SchoolEntity> deptList = schoolService.getSchoolTree();

        //添加一级部门
        if(getUserId() == Constant.SUPER_ADMIN){
            System.out.println("select======================"+getUserId());
            SchoolEntity root = new SchoolEntity();
            root.setId(0L);
            root.setName("学校");
            root.setParentId(-1L);
            root.setOpen(true);
            deptList.add(root);
        }

        return R.ok().put("deptList", deptList);
    }


    /**
     * 上级部门Id(管理员则为0)
     */
    @RequestMapping("/info")
    @RequiresPermissions("course:school:list")
    public R info(){
        long deptId = 0;

        if(getUserId() != Constant.SUPER_ADMIN){
            System.out.println("info======================"+getUserId());
            List<SchoolEntity> deptList = schoolService.queryList(new HashMap<String, Object>());
            Long parentId = null;
            for(SchoolEntity sysDeptEntity : deptList){
                if(parentId == null){
                    parentId = sysDeptEntity.getParentId();
                    continue;
                }

                if(parentId > sysDeptEntity.getParentId().longValue()){
                    parentId = sysDeptEntity.getParentId();
                }
            }
            deptId = parentId;
        }

        return R.ok().put("deptId", deptId);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{schoolId}")
    @RequiresPermissions("course:school:info")
    public R info(@PathVariable("schoolId") Long deptId){
        SchoolEntity dept = schoolService.selectById(deptId);

        return R.ok().put("dept", dept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("course:school:save")
    public R save(@RequestBody SchoolEntity schoolEntity){
        schoolService.insert(schoolEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("course:school:update")
    public R update(@RequestBody SchoolEntity schoolEntity){
        schoolService.updateById(schoolEntity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("course:school:delete")
    public R delete(long deptId){
        //判断是否有子部门
        List<Long> deptList = schoolService.queryDetpIdList(deptId);
        if(deptList.size() > 0){
            return R.error("请先删除子部门");
        }

        schoolService.deleteById2(deptId);

        return R.ok();
    }

}
